import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { Response } from '@angular/http';
import { map } from "rxjs/operators";
@Injectable()
export class ResourceService {

    constructor(private http: HttpClient) { }

    /** 获取系统树状结构 */
    getSystemTree() {
        return this.http.get('/system/api/info/listtree').pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 获取资源树状结构 */
    getResourceTree() {
        return this.http.get('/system/api/resources/listtree').pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 获取指定系统下的资源信息 */
    loadResourcesBySysId(id: number) {
        return this.http.get('/system/api/resources/listbysys?sysId=' + id).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 删除资源 */
    deleteResourceById(id: number) {
        return this.http.get('/system/api/resources/delete?id=' + id).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 加载资源 */
    loadResource(id: number) {
        return this.http.get('/system/api/resources/load?id=' + id).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 显示详情界面 */
    showDetail(id: number, sysId: number) {
        if (sysId) {
            return this.http.get('/system/api/resources/showeditdetail?id=' + id + '&sysId=' + sysId).pipe(
                map((res: Response) => res['data'])
            )
        } else {
            return this.http.get('/system/api/resources/load?id=' + id).pipe(
                map((res: Response) => res['data'])
            )
        }

    }
    /** 保存或更新资源信息 */
    saveUpdateResource(resource: any) {
        return this.http.post('/system/api/resources/saveupdate', resource).pipe(
            map((res: Response) => res['data'])
        )
    }
}
